package org.csu.forum.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.csu.forum.domain.PO.*;
import org.csu.forum.mapper.*;
import org.csu.forum.service.SectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SectionServiceImpl implements SectionService{

    @Autowired
    SectionMapper sectionMapper;

    @Autowired
    ZoneMapper zoneMapper;

    @Autowired
    PostMapper postMapper;

    @Autowired
    FileMapper fileMapper;

    @Autowired
    TitlepostMapper titlepostMapper;

    @Override
    public int addSection(Section section) {
        QueryWrapper<Section>sectionQueryWrapper = new QueryWrapper<>();
        sectionQueryWrapper.eq("sectionname",section.getSectionname());
        if(sectionMapper.selectOne(sectionQueryWrapper) == null){
            sectionMapper.insert(section);
            return sectionMapper.selectOne(sectionQueryWrapper).getSectionid();
        }
            return -1;
    }

    @Override
    public void deleteSection(int sectionid) {
        QueryWrapper<Section>sectionQueryWrapper = new QueryWrapper<>();
        sectionQueryWrapper.eq("sectionid",sectionid);
        sectionMapper.delete(sectionQueryWrapper);
    }

    @Override
    public void editSection(Section section) {
        int sectionid = section.getSectionid();

        UpdateWrapper<Section>sectionUpdateWrapper = new UpdateWrapper<>();
        sectionUpdateWrapper.eq("sectionid",sectionid);

        sectionMapper.update(section,sectionUpdateWrapper);
    }

    @Override
    public List<Section> getSectionList() {
        return sectionMapper.selectList(null);
    }

    @Override
    public int addZones(Zone zone) {
        QueryWrapper<Zone>zoneQueryWrapper = new QueryWrapper<>();
        zoneMapper.insert(zone);
        zoneQueryWrapper.eq("sectionid",zone.getSectionid()).eq("zonename",zone.getZonename());
        return zoneMapper.selectOne(zoneQueryWrapper).getZoneid();
    }

    @Override
    public void deleteZone(int zoneid) {
        QueryWrapper<Zone>zoneQueryWrapper = new QueryWrapper<>();
        zoneQueryWrapper.eq("zoneid",zoneid);
        zoneMapper.delete(zoneQueryWrapper);
    }

    @Override
    public void editZone(Zone zone) {
        int zoneid = zone.getZoneid();

        UpdateWrapper<Zone>zoneUpdateWrapper = new UpdateWrapper<>();
        zoneUpdateWrapper.eq("zoneid",zoneid);

        zoneMapper.update(zone,zoneUpdateWrapper);
    }

    @Override
    public List<Zone> getZoneList(int sectionid) {
        QueryWrapper<Zone>zoneQueryWrapper = new QueryWrapper<>();
        zoneQueryWrapper.eq("sectionid",sectionid);
        return zoneMapper.selectList(zoneQueryWrapper);
    }

    @Override
    public int getSectionOwner(int sectionid) {
        QueryWrapper<Section>sectionQueryWrapper = new QueryWrapper<>();
        sectionQueryWrapper.eq("sectionid",sectionid);
        return sectionMapper.selectOne(sectionQueryWrapper).getSectionownerid();
    }

    @Override
    public void editPostStatus(Titlepost titlepost) {
        UpdateWrapper<Titlepost>titlepostUpdateWrapper = new UpdateWrapper<>();
        titlepostUpdateWrapper.eq("post_id",titlepost.getPostid());
        titlepostMapper.update(titlepost,titlepostUpdateWrapper);
    }

    public Files getFileSrc(int fileid) {
        QueryWrapper<Files>fileQueryWrapper = new QueryWrapper<>();
        fileQueryWrapper.eq("file_id",fileid);
        return fileMapper.selectOne(fileQueryWrapper);
    }

    @Override
    public void deleteTitlePost(int postid) {
        QueryWrapper<Titlepost>titlepostQueryWrapper = new QueryWrapper<>();
        titlepostQueryWrapper.eq("post_id",postid);
        titlepostMapper.delete(titlepostQueryWrapper);
    }

    @Override
    public Titlepost getTitlepost(int postid) {
        QueryWrapper<Titlepost>titlepostQueryWrapper = new QueryWrapper<>();
        titlepostQueryWrapper.eq("post_id",postid);
        return titlepostMapper.selectOne(titlepostQueryWrapper);
    }

    @Override
    public int getSectionIdByZoneId(int zoneid) {
        QueryWrapper<Zone>zoneQueryWrapper = new QueryWrapper<>();
        zoneQueryWrapper.eq("zoneid",zoneid);
        return zoneMapper.selectOne(zoneQueryWrapper).getSectionid();
    }


    @Override
    public List<Files> getFileList(int sectionId) {
        List<Files> filesList = new ArrayList<>();

        QueryWrapper<Zone>queryWrapper = new QueryWrapper<>();
        QueryWrapper<Post>postQueryWrapper = new QueryWrapper<>();
        QueryWrapper<Files>fileQueryWrapper = new QueryWrapper<>();

        queryWrapper.eq("sectionid",sectionId);
        List<Zone>zones = zoneMapper.selectList(queryWrapper);
        for(int i = 0 ; i < zones.size() ; i++){
            int zoneid = zones.get(i).getZoneid();
            postQueryWrapper.clear();
            postQueryWrapper.eq("zoneid",zoneid);
            List<Post>posts = postMapper.selectList(postQueryWrapper);
            for(int j = 0 ; j < posts.size() ; j++){
                int fileid = posts.get(j).getFileid();
                fileQueryWrapper.clear();
                fileQueryWrapper.eq("file_id",fileid);
                filesList.add(fileMapper.selectOne(fileQueryWrapper));
            }
        }
        return filesList;
    }
}
